package com.managedbean;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.service.UserEJB;
import com.woupla.model.Customer;
import com.woupla.model.Manager;
import com.woupla.model.User;

import java.io.Serializable;


/**
 * 
 */

/**
 * @author Dimitri Lebel
 *
 */
@Named
@SessionScoped
public class UserMBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4445950074774092185L;
	@EJB 
	private UserEJB userEJB;
	private User user ;
	private List<User> users;
	private Boolean manager;
	@Inject @Named
	private CompanyMBean companyMBean;
	
	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}
	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		users = userEJB.getAllUsers();
		return users;
	}
	/**
	 * @param users the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	/**
	 * @return the manager
	 */
	public Boolean getManager() {
		return manager;
	}
	/**
	 * @param manager the manager to set
	 */
	public void setManager(Boolean manager) {
		this.manager = manager;
		if (manager){
			user = new Manager();
		}
	}
	/**
	 * @return the companyMBean
	 */
	public CompanyMBean getCompanyMBean() {
		return companyMBean;
	}
	/**
	 * @param companyMBean the companyMBean to set
	 */
	public void setCompanyMBean(CompanyMBean companyMBean) {
		this.companyMBean = companyMBean;
	}
	public UserMBean() {
		user = new Customer();
		users = new ArrayList<User>();
	}
	 @PostConstruct
	 public void reset(){
		 manager = false;
	 }
	
	public void createUser(){
		if(manager){
			Manager manager = (Manager) user;
			manager.setCompany(companyMBean.getCompany());
			
		}
		userEJB.saveUser(user);
	}
	
	public void connectUser(){
		userEJB.findUser(user);
	}


	
}
